<script lang="ts" setup>
import to from 'await-to-js'
import type { FormInstance } from 'element-plus'
import type { UserInfo, HandleType } from './data'
import { addUser, editUser } from '@system/api/system/user'

defineOptions({
  name: 'UserDetail'
})
const emit = defineEmits(['close'])
const props = defineProps<{
  currentRow: UserInfo | undefined
  type: HandleType
}>()
const title = props.type === 'add' ? '新建用户' : props.type === 'edit' ? '编辑用户' : '用户详情'
const isLoading = ref(false)
const ruleFormRef = ref<FormInstance>()
const dialogVisible = ref<boolean>(true)

const data = reactive({
  ruleForm: {},
  rules: {
    userId: [{ required: true, message: '请填写' }],
    roleIds: [{ required: true, message: '请选择' }]
  }
})
const { ruleForm, rules } = toRefs<any>(data)

const handleSubmit = async () => {
  const validate = await to(ruleFormRef.value.validate())
  if (!validate[1]) return
  const data = JSON.parse(JSON.stringify(ruleForm.value))
  isLoading.value = true
  const [err] = await to(props.type === 'add' ? addUser(data) : editUser(data))
  isLoading.value = false
  if (err) return
  ElMessage.success('操作成功')
  emit('close', true)
}

onMounted(() => {
  if (props.currentRow) {
    const data = JSON.parse(JSON.stringify(props.currentRow))
    ruleForm.value = data
  }
})
</script>

<template>
  <el-dialog v-model="dialogVisible" :title="title" width="900px" class="one-dialog" draggable @close="emit('close')">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="110px"
      :disabled="type === 'detail'"
      class="mr-10 one-edit-form">
      <el-row :gutter="10">
        <el-col :lg="12" :span="24">
          <el-form-item label="登录用户名:" prop="userId">
            <el-input v-model="ruleForm.userId" placeholder="登录用户名" maxlength="15" class="w-full" />
          </el-form-item>
        </el-col>
        <el-col :lg="12" :span="24">
          <el-form-item label="手机号:" prop="phoneNo">
            <el-input v-model="ruleForm.phoneNo" placeholder="手机号" maxlength="11" class="w-full" />
          </el-form-item>
        </el-col>
        <el-col :lg="12" :span="24">
          <el-form-item label="邮箱:" prop="email">
            <el-input v-model="ruleForm.email" placeholder="邮箱" maxlength="60" class="w-full" />
          </el-form-item>
        </el-col>
        <el-col :lg="12" :span="24">
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="ruleForm.username" placeholder="姓名" maxlength="6" class="w-full" />
          </el-form-item>
        </el-col>
        <el-col :lg="12" :span="24">
          <el-form-item label="是否启用:" prop="isDisabled">
            <el-switch v-model="ruleForm.isDisabled" :active-value="0" :inactive-value="1" />
          </el-form-item>
        </el-col>
        <el-col :lg="24" :span="24">
          <el-form-item label="备注:" prop="remark">
            <el-input v-model="ruleForm.remark" placeholder="备注" type="textarea" class="w-full" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit()" :loading="isLoading">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
./data
